package tw.com.com;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import tw.com.bean.DTO;
import tw.com.bean.Stock;
import tw.com.com.MyWriter;
import tw.com.web.CommonContent;

public class ExcelOut implements MyWriter {

	private WritableWorkbook workbook = null;

	@Override
	public void outPut(DTO dto) throws Exception {

		try {

			workbook = Workbook.createWorkbook(new File(dto.getFundId()
					+ ".xls"));

			WritableSheet sheet = workbook.createSheet(dto.getFundId(), 0);

			Map<String, List<Stock>> stockMap = dto.getMaps();
			int colume = 0;
			int row = 1;
			int extractNum = 0;
			int extractMove = 6;

			for (String yyy : stockMap.keySet()) {

				List<Stock> stocks = stockMap.get(yyy);

	

				extractNum++;

				for (Stock stock : stocks) {
					Integer monSub3 = Integer.valueOf(stock.getDate().getMm()) % 3;
					
					Integer monSub3Va = (Integer.valueOf(stock.getDate()
							.getMm()) - 1) / 3;
					
					sheet.addCell(new Label(monSub3Va+extractNum * extractMove, 0, yyy));
					Integer mon = Integer.valueOf(stock.getDate().getMm());
					Integer day = Integer.valueOf(stock.getDate().getDd());

					System.out.println("mm=" + mon + " " + "dd=" + day);
					//
					// if(mon!=1&&mon%3==1&&day==1){
					// colume+=2;
					// }

					if (monSub3 == 2) {
						row = 33 + day;
					}
					if (monSub3 == 1) {
						row = 1 + day;
					}
					if (monSub3 == 0) {
						row = 66 + day;
					}

					colume = monSub3Va + extractNum * extractMove;

					// System.out.println("row"+row+" "+ "colume="+colume);
					sheet.addCell(new Label(colume, row, stock.getDate().getYyy()+"/"+stock.getDate()
							.getMm()
							+ "/"
							+ stock.getDate().getDd()
							+ "="
							+ stock.getSockVal()));

				}
				// colume++;

			}

			workbook.write();
			System.out.print("end");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			workbook.close();
		}
	}

	public void getColume(int month) {

	}
}
